<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <BlacklistDrawer @register="registerBlacklistDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="blackList">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import BlacklistDrawer from './components/BlacklistDrawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

import { columns, searchFormSchema } from './data/data';
import { Api, changeEnable } from './data/api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const { createMessage, createConfirm } = useMessage();

const [registerBlacklistDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'blacklist-template',
  tableProps: {
    //表格标题
    title: '黑名单列表',
    //主键字段
    rowKey: 'blacklistId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //默认排序(排序字段上需设置defaultSortOrder:'descend')
    /*defSort: {
      order: 'createTime',
      sort: 'desc',
    },*/
    //操作列配置
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '启用',
      onClick: handleChangeEnable.bind(null, record, 1),
      ifShow: _action => {
        return record.isEnable == 0;
      },
    },
    {
      label: '不启用',
      onClick: handleChangeEnable.bind(null, record, 0),
      ifShow: _action => {
        return record.isEnable == 1;
      },
    },
  ];
}

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleChangeEnable(record: Recordable, isEnable) {
  let blacklistId = record.blacklistId;
  let desc = isEnable == 0 ? '不启用' : '启用';
  createConfirm({
    iconType: 'info',
    title: '确认' + desc,
    content: '确认' + desc + '该条黑名单',
    onOk: async () => {
      await changeEnable({ blacklistId: blacklistId, isEnable: isEnable });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}
</script>
